package GUI;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;

import IO.RoutersIO;
import Roteador.Router;


/**
 * Classe responsável pela interface gráfica.
 * @author David, Iury, Milena
 *
 */
public class GUIShowStatus extends JFrame {
	private static final long serialVersionUID = 3638019996542562884L;
	private JTextArea area ;
	private JTable tabela;

	/**
	 * Inicializa a interface gráfica.
	 * @param r roteador a ser representado.
	 */
	public GUIShowStatus(Router r) {
		setTitle("Detalhes do roteador: " + r.getId());
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		JPanel hPanel = new JPanel();
		hPanel.setLayout(new BoxLayout(hPanel, BoxLayout.X_AXIS));
		JPanel vPanel = new JPanel();
		vPanel.setLayout(new BoxLayout(vPanel, BoxLayout.Y_AXIS));

		//hPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

		JScrollPane textPane = new JScrollPane();

		area = new JTextArea(); 

		area.setLineWrap(true);
		area.setWrapStyleWord(true);
		area.setEditable(false);
		textPane.getViewport().add(area);

		String[][] data = {};
		String[] colunas = { "Destino", "Custo", "Pr�ximo Roteador" };

		tabela = new JTable();
		
		tabela.setModel(new DefaultTableModel(data, colunas));
		tabela.setEnabled(false);
		JScrollPane tabelaPane = new JScrollPane(tabela);
		
		JButton close = new JButton("Sair");
		close.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				System.exit(0);
			}
		});
		
		hPanel.add(tabelaPane);
		hPanel.add(textPane);
		
		vPanel.add(hPanel);
		vPanel.add(close);
		add(vPanel);
		
		setSize(new Dimension(600, 600));

		setLocationRelativeTo(null);
		setVisible(true);

	}

	/**
	 * Adiciona uma mensagem ao campo de mensagens.	 * 
	 * @param message  A nova mensagem.
	 */
	public void recebeDados(String message) {

		final String fmessage = message;
		SwingUtilities.invokeLater(
				new Runnable() {
					public void run() {
						area.setText(area.getText() + "\n" + fmessage);
					}
				});

	}
	
	/**
	 * Atualiza a tabela de roteamento da esquerda, para um dado nó.
	 * @param dados novos dados
	 */
	public void atualizaTabela(String[][] dados) {
		final String[][] data = dados;
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				String[] colunas = { "Fonte", "Destino", "Custo" };
				tabela.setModel(new DefaultTableModel(data, colunas));
			}

		});
	}


}
